package com.igoal.dto;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

import com.igoal.entity.News;
import com.igoal.entity.Taggable;
import com.igoal.service.paging.PageItem;
import com.igoal.utils.CalendarUtils;
import com.igoal.utils.StringUtils;
import com.igoal.utils.SynchronizeAttributeWithOther;
import com.igoal.utils.SynchronizeDirection;
import com.igoal.web.Constants;
import com.igoal.web.validator.FileUploadable;

public class NewsDTO extends BaseDTO<News> implements Serializable,
		PageItem<Long>, Commentable, Taggable, FileUploadable, SEOLink {
	private static final long serialVersionUID = 4439224508228681698L;

	private Integer index;
	private String title;
	private String body;
	private String keywords;
	private String description;
	private String intro;
	private String avatar;
	private Integer commentCount = 0;
	private Integer likeCount = 0;
	private boolean isPublic;
	private Long sortedValue;
	
	private List<CommentDTO> comments;
	private List<TagDTO> tags;
	private String tagNames;

	private MultipartFile avatarFile;
	
	public MultipartFile getAvatarFile() {
		return avatarFile;
	}

	public void setAvatarFile(MultipartFile avatarFile) {
		this.avatarFile = avatarFile;
	}

	public boolean isPublic() {
		return isPublic;
	}
	
	public boolean getPublic() {
		return isPublic;
	}

	public void setPublic(boolean isPublic) {
		this.isPublic = isPublic;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public Integer getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(Integer commentCount) {
		this.commentCount = commentCount;
	}

	public void setSortedValue(Long sortedValue) {
		this.sortedValue = sortedValue;
	}

	public void setComments(List<CommentDTO> comments) {
		this.comments = comments;
	}

	public List<CommentDTO> getComments() {
		return this.comments;
	}

	public Long getSortedValue() {
		return sortedValue;
	}

	public Integer getLikeCount() {
		return likeCount;
	}

	public void setLikeCount(Integer likeCount) {
		this.likeCount = likeCount;
	}

	public void setYouLikeThis(boolean likeThis) {
		this.isYouLikeThis = likeThis;
	}

	public boolean getYouLikeThis() {
		return this.isYouLikeThis;
	}

	@SynchronizeAttributeWithOther(direction = SynchronizeDirection.FROM, value = "createDate")
	public void initUpdatedSinceFromEntity(Object entity) {
		if (entity != null && entity instanceof Date) {
			Date date = (Date) entity;
			this.updatedSince = CalendarUtils.calculateMinutesDifferentBeforeNow(date);
			this.sortedValue = date.getTime();
		}
	}

	public void setTags(List<TagDTO> tags) {
		this.tags = tags;
	}

	public List<TagDTO> getTags() {
		return tags;
	}

	public String[] getListTagNames() {
		String[] listTagNames = new String[0];
		
		if(!StringUtils.isBlank(tagNames)){
			listTagNames = tagNames.split(Constants.TAG_DELIMITER);
		}
		
		return listTagNames;
	}

	public String getTagNames() {
		return tagNames;
	}

	public void setTagNames(String tagNames) {
		this.tagNames = tagNames;
	}

	public MultipartFile getFile() {
		return avatarFile;
	}

	public String getFileField() {
		return "avatarFile";
	}
	
	public String getSEOLink() {
		String seo = StringUtils.convertToSEOLink(title);
		return seo;
	}
}
